/*
+--------------------------------------------------------------------------
|   Mblog [#RELEASE_VERSION#]
|   ========================================
|   Copyright (c) 2014, 2015 mtons. All Rights Reserved
|   http://www.mtons.com
|
+---------------------------------------------------------------------------
*/
package com.hefei.blog.admin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hefei.blog.admin.data.Admin;
import com.hefei.blog.admin.persist.service.AdminService;
import com.hefei.blog.admin.persist.service.RoleService;
import com.hefei.blog.base.paging.Paging;
import com.hefei.blog.web.controller.BaseController;

@Controller("mng_manager_ctl")
@RequestMapping("/admin/manager")
public class ManagerController extends BaseController {
	@Autowired
	private AdminService adminService;
	
	@Autowired
	private RoleService roleService;

	@RequestMapping("/list")
	public String list(Integer pn, String key, ModelMap model) {
		Paging page = wrapPage(pn);
		adminService.paging(page, key);
		model.put("page", page);
		model.put("key", key);
		return "/admin/manager/list";
	}

	@RequestMapping("/view")
	public String view(Long id, ModelMap model) {
		Admin view = adminService.get(id);
		model.put("view", view);
		model.put("roles", roleService.getAll());
		return "/admin/manager/view";
	}

	@RequestMapping("/update_role")
	public String update(long id, String roleIds) {
		String[] roleStrIds = roleIds.split(",");
		Long[] ids = new Long[roleStrIds.length];
		for(int i=0;i<ids.length;i++){
			ids[i] = Long.valueOf(roleStrIds[i]);
		}
		adminService.updateRole(id, ids);
		return "redirect:/admin/users/list";
	}

	@RequestMapping(value = "/pwd", method = RequestMethod.GET)
	public String pwsView(Long id, ModelMap model) {
		Admin view = adminService.get(id);
		model.put("view", view);
		return "/admin/manager/pwd";
	}
//
//	@RequestMapping(value = "/pwd", method = RequestMethod.POST)
//	public String pwd(Long id, String newPassword, ModelMap model) {
//		Admin ret = adminService.get(id);
//		model.put("view", ret);
//
//		try {
//			adminService.updatePassword(id, newPassword);
//			model.put("message", "修改成功");
//		} catch (IllegalArgumentException e) {
//			model.put("message", e.getMessage());
//		}
//		return "/admin/users/pwd";
//	}

//	@RequestMapping("/open")
//	public @ResponseBody Data open(Long id) {
//		adminService.updateStatus(id, EntityStatus.STATUS_NORMAL);
//		Data data = Data.success("操作成功", Data.NOOP);
//		return data;
//	}
//
//	@RequestMapping("/close")
//	public @ResponseBody Data close(Long id) {
//		adminService.updateStatus(id, EntityStatus.STATUS_CLOSED);
//		Data data = Data.success("操作成功", Data.NOOP);
//		return data;
//	}
}
